/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.ActivityComponent;
import sz.szsmk.citizencard.ioc.dzsmkcompontent.DaggerActivityComponent;
import sz.szsmk.citizencard.ioc.dzsmkmodule.ActivityModule;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.retrofitrxjava.DZSmkAppInit;

/**
 * <br /> author: chenshufei
 * <br /> date: 16/1/15
 * <br /> email: chenshufei2@sina.com
 */
public abstract class AppStatusBarBaseActivity extends BaseActivity {

    public static String TAG = "BaseActivity";

    private int mNavigationIcon = -1;
    private Drawable mNavigationDrawable;


    private Toolbar mToolbar;
    private TextView mTitleTextView;
    private ImageView mIv_center_log;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        initSystemBar();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            // 透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            // 透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//        }
       //第二个参数是想要设置的颜色
//        if (Build.VERSION.SDK_INT >= 21) {
//            View decorView = getWindow().getDecorView();
//            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            getWindow().setStatusBarColor(Color.TRANSPARENT);
//        }
//        StatusBarCompat.compat(this, getResources().getColor(R.color.TRANSPARENT));
        //mNavigationIcon = R.drawable.navbar_back_grey;
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
//        initSystemBar();

    }

    protected void initSystemBar() {
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0.2f)
                .statusBarColor(R.color.white).init();
    }

    @Override
    public void onContentChanged() {
        super.onContentChanged();
        View toolView = findViewById(R.id.toolbar);
        if (null != toolView){
            mToolbar = (Toolbar) toolView;
//            setSupportActionBar(mToolbar);
            if (getSupportActionBar() != null){
                getSupportActionBar().hide();
            }
            mTitleTextView = (TextView) toolView.findViewById(R.id.tv_title);
            mIv_center_log = (ImageView) toolView.findViewById(R.id.iv_center_log);

            CharSequence title = getTopTitle();
            if (null != title && title.length()>0){
                mTitleTextView.setText(title);
            }

            if (-1 != mNavigationIcon){
                mToolbar.setNavigationIcon(mNavigationIcon);
            }
            if (null != mNavigationDrawable){
                mToolbar.setNavigationIcon(mNavigationDrawable);
            }
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickNavigationIcon();
                }
            });

        }
    }

    protected abstract CharSequence getTopTitle() ;

    protected void onClickNavigationIcon() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 解决activity 依赖的注入 组件
     * @return
     */
    public ActivityComponent getComponent(){
        ActivityComponent component = DaggerActivityComponent.builder()
                                        .activityModule(new ActivityModule(this))
                                        .applicationComponent(DZSmkAppInit.getInstance().getComponent())
                                        .build();
        return component;
    }

    //----- 处理ToolBar 右边的 菜单 开始 ---------


    @Override
    public void onUnLogin() {
        super.onUnLogin();
        //launch(LoginActivity.class);
        eventBus.post(new MessageEvent(MessageEvent.CODE_UNLOGIN,""));
        finish();
    }

    //----- 处理ToolBar 右边的 菜单 结束 ---------

    public void setTopTitle(String title){
        if (null != mTitleTextView){
            mTitleTextView.setText(title);
        }
    }
}
